package easy;

import java.util.ArrayList;
import java.util.List;

public class Solution_1408 {

    public List<String> stringMatching(String[] words) {
        List<String>[] array = new List[31];
        for (String word : words) {
            List<String> list = array[word.length()];
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(word);
            array[word.length()] = list;
        }
        List<String> res = new ArrayList<>();
        for (String word : words) {
            boolean flag = false;
            for (int i = word.length() + 1; i < array.length; i++) {
                List<String> list = array[i];
                if (list != null) {
                    for (String s : list) {
                        if (s.contains(word)) {
                            flag = true;
                            break;
                        }
                    }
                }
                if (flag) {
                    break;
                }
            }
            if (flag) {
                res.add(word);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution_1408 model = new Solution_1408();
        System.out.println(model.stringMatching(new String[]{"mass", "as", "hero", "superhero"}));
        System.out.println(model.stringMatching(new String[]{"leetcode", "et", "code"}));
        System.out.println(model.stringMatching(new String[]{"blue", "green", "bu"}));
    }
}
